package webdev.adapters;

import java.util.ArrayList;

import webdev.dto.CommentDTO;
import webdev.dto.DiaDiemDTO;
import android.content.Context;
import android.util.SparseIntArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

public class PlaceDetailAdapter extends BaseAdapter {

	private static final int TYPE_CONTENT_PLACE_DETAIL = 0;
	private static final int TYPE_LIST_COMMENT = 1;
	private static final int TOTAL = 2;
	
	private ArrayList<Integer> indexTypePlace = new ArrayList<Integer>();
	private SparseIntArray indexTypeContentPlace = new SparseIntArray();
	private SparseIntArray indexTypeListComment = new SparseIntArray();
	
	// Adapter
	private ListPlaceContentAdapter mListPlaceContentAdapter;
	private ListPlaceViewCommentAdapter mListCommentAdapter;
	
	private Context mContext;
	
	public PlaceDetailAdapter(Context _context) {
		this.mContext = _context;
		// Initialize adatper
		mListPlaceContentAdapter = new ListPlaceContentAdapter(_context);
		mListCommentAdapter = new ListPlaceViewCommentAdapter(_context);
	}
	
	public void addContentPlace(DiaDiemDTO _place) {
		mListPlaceContentAdapter.addContent(_place);
		indexTypePlace.add(TYPE_CONTENT_PLACE_DETAIL);
		indexTypeContentPlace.put(indexTypePlace.size() - 1, mListPlaceContentAdapter.getCount() - 1);
	}
	
	public void addListComment(CommentDTO comment) {
		mListCommentAdapter.addComment(comment);
		indexTypePlace.add(TYPE_LIST_COMMENT);
		indexTypeListComment.put(indexTypePlace.size() - 1, mListCommentAdapter.getCount() - 1);
	}
	
	@Override
	public int getCount() {
		return indexTypePlace.size();
	}
	
	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public int getViewTypeCount() {
		return TOTAL;
	}
	
	@Override
	public int getItemViewType(int position) {
		return indexTypePlace.get(position);
	}
	
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// Get type of item
		int type = getItemViewType(position);
		
		switch (type) {
		case TYPE_CONTENT_PLACE_DETAIL:
			convertView = mListPlaceContentAdapter.getView(indexTypeContentPlace.get(position), convertView, parent);
			break;
		case TYPE_LIST_COMMENT:
			convertView = mListCommentAdapter.getView(indexTypeListComment.get(position), convertView, parent);
			break;
		default:
			break;
		}
		return convertView;
	}
	
}
